package org.afun.csms_android;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class CameraActivity extends AppCompatActivity {

    private SurfaceView sf;
    private Camera camera;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera);
        findViewById(R.id.bt).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                camera.takePicture(null, null, new Camera.PictureCallback() {//开始拍照；
                    @Override
                    public void onPictureTaken(byte[] data, Camera camera) {//拍完之后回调；
                        String path = null;

                        if ((path = savephoto(data)) != null) {
                            Intent in = new Intent(CameraActivity.this, MyActivity.class);
                            in.putExtra("path", path);
                            startActivity(in);
                        } else {
                            Toast.makeText(CameraActivity.this, "save photo fail", Toast.LENGTH_LONG).show();
                        }

                    }
                });
            }
        });


        sf = findViewById(R.id.sf);
        sf.getHolder().addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                start();
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                stop();
            }
        });

    }

    private String savephoto(byte[] bytes) {
        try {
            File f = File.createTempFile("img", "");//前缀，后缀
            FileOutputStream fos = new FileOutputStream(f);
            fos.write(bytes);
            fos.flush();
            fos.close();
            return f.getAbsolutePath();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void start() {
        camera = Camera.open();
        try {
            camera.setPreviewDisplay(sf.getHolder());
            camera.startPreview();//开始预览画面
            camera.setDisplayOrientation(90);//拍摄画面旋转90度
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void stop() {
        camera.stopPreview();
        camera.release();
    }
}